package com.learn.transactional.service.impl;

import com.learn.transactional.dao.mapper.StudentMapper;
import com.learn.transactional.dao.pojo.Student;
import com.learn.transactional.service.StudentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    public StudentMapper studentMapper;

    /**
     * @Transactional: 注解，给方法添加事务
     * @param student
     * @return
     */
    @Transactional
    @Override
    public int addStudent(Student student) {
        System.out.println("业务方法 addStudent");
        int rows = studentMapper.insert(student);
        System.out.println("执行sql 语句");
        // 抛出个运行时异常，用于回滚事务
//        int m = 10 /0;
        return rows;
    }

    @Override
    public Student queryStudent(Integer id) {
        Student student = studentMapper.selectByPrimaryKey(id);
        return student;
    }

}
